package com.zxtech.zzm.zxmonitorsocket.resolver

import com.zxtech.zzm.zxmonitorsocket.SocketPackage
import java.text.SimpleDateFormat

class HeartBeatResolver : SocketBaseResolver() {
  override fun resolve(content: Any) {
    heartBeatReceivedTime = System.currentTimeMillis()
    val socketPackage: SocketPackage = content as SocketPackage
    val timeStr: String = socketPackage.packageContent!!.substring(2)
    val sdf = SimpleDateFormat("yyyyMMddHHmmss")
    try {
      val date = sdf.parse(timeStr)
      timeDiff = date.time - System.currentTimeMillis()
      //用于校准时间 or 计算延迟
    } catch (e: Exception) {
      e.printStackTrace()
    }
  }

  companion object {
    var timeDiff: Long = 0
    var heartBeatReceivedTime: Long = 0
      private set

    private val ourInstance = HeartBeatResolver()

    fun handlePackage(socketPackage: SocketPackage?) {
      ourInstance.resolve(socketPackage!!)
    }
  }
}